package com.gangan.controller.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gangan.controller.common.BaseController;
import com.gangan.model.Role;
import com.gangan.model.search.RoleSearch;
import com.gangan.service.IAuthService;
import com.gangan.service.IRoleService;
import com.gangan.tools.MessageBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 角色模块操作控制层
 */
@Api(description = "*角色模块控制层")
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController {
    @Autowired
    IRoleService roleService;
    @Autowired
    IAuthService authService;

    /**
     * 查询所有角色
     * TODO:尚未加入权限控制
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ApiOperation(value="角色列表", notes="拥有权限的用户可查询所有角色")
    public MessageBean list(RoleSearch search){
        IPage<Role> roleIPage = roleService.page(new Page<>(search.getCurrentPage(),search.getPageSize()));
        Map<String,Object> map = new HashMap<>();
        map.put("views",roleIPage.getRecords());
        map.put("total",roleIPage.getTotal());
        return resultSuccess(map);
    }

    /**
     * TODO:添加权限控制
     * @param role
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation(value="新增角色", notes="新增角色")
    public MessageBean add(Role role){
        return roleOperaction(role);
    }

    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @ApiOperation(value="修改角色", notes="修改角色")
    public MessageBean edit(Role role){
        if(null!=role.getRoleId()||role.getRoleId()<0){
            return resultFailed("请选择有效的角色.");
        }
        return roleOperaction(role);
    }

    @RequestMapping(value = "/del",method = RequestMethod.POST)
    @ApiOperation(value="删除角色", notes="删除角色")
    public MessageBean del(Integer roleId){
        if(null==roleService.getById(roleId)){
            return resultFailed("请选择有效的角色.");
        }else if(!roleService.removeById(roleId)){
           return resultFailed();
        }
        return resultSuccess();
    }
    /**
     * 新增/修改操作
     * @param role
     * @return
     */
    public MessageBean roleOperaction(Role role){
        if(StringUtils.isBlank(role.getRoleAuthIds())){
           return resultFailed("请选择至少一项权限.");
        }else if(null!=role.getRoleId()&&!roleService.updateById(role)){
            return resultFailed();
        }else if(null==role.getRoleId()&&!roleService.save(role)){
            return resultFailed();
        }
        return resultSuccess();
    }


}
